﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Aj.Core;

public partial class Manager_SysUserEdit : AuthPageBase
{
    protected int id;
    protected string type = "1";
    protected string username = "";
    protected string moduleids = "";
    protected DataSet ds = null;
    protected DataSet moduleDs = null;

    string password = "";

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        try
        {
            id = QueryParamInt32("id");
            type = QueryParam("type", "1");
            if (id > 0) //更新
            {
                ds = CustomQuery.SysUser_GetUserById(id);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    username = ds.Tables[0].Rows[0]["username"].ToString();
                    moduleids = ds.Tables[0].Rows[0]["moduleids"].ToString();
                    password = ds.Tables[0].Rows[0]["password"].ToString();
                }
            }
            if (type == "1" || type == "3")
            {
                moduleDs = CustomQuery.SysModule_GetModuleByName("");
            }
            if (IsPost)
            {
                // 更新
                if (id > 0)
                {
                    if (ds != null && ds.Tables.Count > 0)
                    {
                        int rel = 0;
                        if (type == "2")      // 修改密码
                        {
                            if (password == Utility.Encrypt(FormParam("oldpassword")))
                            {
                                if (FormParam("newpassword") == FormParam("dnewpassword"))
                                {
                                    rel = CustomQuery.SysUser_UpdatePassword(id, FormParam("pid"));
                                }
                                else
                                {
                                    MessageBox("两次密码不一样！");
                                    return;
                                }
                            }
                            else
                            {
                                MessageBox("老密码密码错误！");
                                return;
                            }
                        }
                        else if (type == "3") // 修改权限
                        {
                            rel = CustomQuery.SysUser_UpdateModuleid(id, FormParam("ckModuleid"));
                        }
                        if (rel > 0)
                        {
                            Page_GoTo("SysUsers.aspx", true);
                        }
                        else
                        {
                            MessageBox("修改失败！");
                            return;
                        }
                    }
                }
                else // 新建
                {
                    if (type == "1")      // 新建
                    {
                        // 先检查一下
                        if (FormParam("password") == FormParam("dpassword"))
                        {
                            ds = CustomQuery.SysUser_GetUserByUname(FormParam("username"));
                            if ((ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) || FormParam("username").ToUpper() == System.Configuration.ConfigurationManager.AppSettings["SuperuserName"].ToUpper())
                            {
                                MessageBox("该用户名[" + FormParam("username") + "]已经存在了！");
                            }
                            else
                            {
                                int rel = CustomQuery.SysUser_Add(FormParam("username"), FormParam("password"), FormParam("ckModuleid"), User.Identity.Name);
                                if (rel > 0)
                                {
                                    Page_GoTo("SysUsers.aspx", true);
                                }
                                else
                                {
                                    MessageBox("新建失败！");
                                    return;
                                }
                            }
                        }
                        else
                        {
                            MessageBox("两次密码不一样！");
                            return;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Utility.WriteErrorLog(ex);
        }
    }
}